package com.example.yk2021xm04.controller;


import com.example.yk2021xm04.model.RespBean;
import com.example.yk2021xm04.pojo.File;
import com.example.yk2021xm04.pojo.User;
import com.example.yk2021xm04.query.FileQuery;
import com.example.yk2021xm04.query.NoticeQuery;
import com.example.yk2021xm04.service.IFileService;
import com.example.yk2021xm04.utils.IDUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LiYongle
 * @since 2021-11-30
 */
@Controller
@RequestMapping("/file")
public class FileController {

    @Resource
    private IFileService fileService;

    /**
     * 资产管理主界面
     * @return
     */
    @RequestMapping("index")
    public String index() {
        return "file/file";
    }

    /**
     * 添加更新资产页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("addOrUpdateFilePage")
    public String addOrUpdatePage(Integer id, Model model) {
        if (null!=id) {
            model.addAttribute("file",fileService.getById(id));
        }
        return "file/add_update";
    }

    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> fileList(FileQuery fileQuery) {
        return fileService.fileList(fileQuery);
    }

    @RequestMapping("delete")
    @ResponseBody
    public RespBean deleteFile(Integer[] ids) {
        fileService.deleteFile(ids);
        return RespBean.success("文件记录删除成功");
    }

    @RequestMapping("add")
    @ResponseBody
    public RespBean addFile(File file) {
        fileService.addFile(file);
        return RespBean.success("文件记录添加成功");
    }


    @RequestMapping("update")
    @ResponseBody
    public RespBean updateFile(File file) {
        fileService.updateFile(file);
        return RespBean.success("文件记录更新成功");
    }

    @RequestMapping("count")
    public String statistic() {
        return "file/file-statistics";
    }

    @RequestMapping("statistic")
    @ResponseBody
    public RespBean fileStatistic() {
        //发布时间分组 group by day
        List<Object> registerTimeByDay = fileService.sendTimeByDay();
        //一天中的发布任务数
        List<Object> registerCountByDay = fileService.sendCountByDay();
        //日同比
        double rate = 0.0;
        //总任务数量
        Integer totalFileCount = fileService.count();
        //今天的发布任务数量
        Integer FileCountInDay = fileService.FileCountInDay();
        //昨天的发布任务数量
        Integer FileCountInYesterday = fileService.FileCountInYesterday();

        rate = (double)(FileCountInDay - FileCountInYesterday) / FileCountInYesterday;
        if(FileCountInYesterday == 0) {
            rate = 0;
        }
        Map<String, Object> res = new HashMap<>();
        res.put("num", registerCountByDay);
        res.put("time", registerTimeByDay);
        res.put("total", totalFileCount);
        res.put("dayCount", FileCountInDay);
        res.put("rate", rate);
        return RespBean.success("任务统计数据返回成功",res);
    }
}
